home *** CD-ROM | disk | FTP | other *** search
- #include "uucp.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #ifdef BSD42
- #include <sys/dir.h>
- #endif BSD42
- #ifdef NDIR
- #include <ndir.h>
- #endif NDIR
- #ifdef LIBNDIR
- #include "LIBNDIR/ndir.h"
- #endif LIBNDIR
-
- /*
- * uumailclean - this program searches through the uucp spool directory
- * looking for mail files. Files which have been around for longer
- * than "failtime" hours will be returned to the sender. If a file
- * has been around longer than "warntime" hours, then a warning
- * message is sent (once) to the sender.
- *
- * If you use L.dirs to specify subdirectories, then failtime and
- * warntime can be specified as 3rd and 4th arguments to the directory
- * entries respectively: e.g. "C. 336 168 72"
- * By default, these times are 1 week and 3 days.
- *
- * Written by Jim Crammond <jim@cs.hw.ac.uk> 3/86
- */
-
- #define FAILTIME 168 /* default hours before returning the mail */
- #define WARNTIME 72 /* default hours before sending a warning */
-
- #define WARNFILE UUCPDIR/uucp/warnlist.mail"
-
- int warntime, failtime;
-
- main(argc, argv)
- char *argv[];
- {
- #ifdef SUBCS
- FILE *fdirs;
- #endif SUBCS
- char file[NAMESIZE];
- char cdir[ MAXFULLNAME ];
- char *flds[10];
- int nflds, ret;
- int orig_uid = getuid();
-
- strcpy(Progname, "uumailclean");
- uucpname(Myname);
- chkdebug(orig_uid);
-
- /* cd to spool */
- ASSERT(subchdir(Spool) != -1, "cannot chdir to ", Spool, 0 );
-
- init_warnedlist(WARNFILE);
-
- #ifdef SUBCS
- fdirs=fopen(DIRFILE,"r");
- ASSERT(fdirs != NULL, "uumailclean cannot open", DIRFILE, 0);
-
- while (cfgets(cdir, sizeof(cdir), fdirs) != NULL)
- { nflds = getargs( cdir, flds );
- ASSERT(nflds >= 1, "BAD entry in", DIRFILE, 0);
-
- /* only interested in command files */
- if (flds[0][0] != CMDPRE)
- continue;
-
- failtime = (nflds > 2) ? atoi(flds[2]) : FAILTIME;
- warntime = (nflds > 3) ? atoi(flds[3]) : WARNTIME;
-
- checkfiles(flds[0]);
- }
- #else SUBCS
- failtime = FAILTIME;
- warntime = WARNTIME;
-
- checkfiles(".");
- #endif SUBCS
-
- exit(0);
- }
-
-
- /*
- * checkfiles - scan a directory looking for "old" control files.
- * For each one found, call fail or warn as appropriate.
- */
- checkfiles(dir)
- char *dir;
- {
- DIR *dirp;
- char file[NAMESIZE];
- struct stat stbuf;
- time_t now;
- int hours;
-
- time(&now);
-
- DEBUG(5, "checkfiles(%s)\n", dir);
- if ((dirp = opendir(dir)) == NULL)
- { printf("directory unreadable\n");
- return;
- }
-
- while (gnamef(dirp, file))
- { if (file[0] != CMDPRE)
- continue;
-
- if (stat(subfile(file), &stbuf) == -1)
- { DEBUG(4, "stat on %s failed\n", file);
- continue;
- }
-
- if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
- continue;
-
- hours = (int) (now - stbuf.st_mtime) / 3600;
- if (hours >= failtime)
- fail(file, hours);
- else if (hours >= warntime)
- warn(file, hours);
- }
- }
-
-
- /*
- * fail - send a failure message to the sender and delete the mail.
- */
- fail(cmdfile, hours)
- char *cmdfile;
- int hours;
- {
- char dfile[NAMESIZE], xfile[NAMESIZE];
- char host[NAMESIZE];
- char *from, **to;
- char *sender(), **recipients();
-
- DEBUG(4, "fail called on %s\n", cmdfile);
- getfnames(cmdfile, dfile, xfile);
-
- if ((to = recipients(xfile)) == NULL)
- return;
- if ((from = sender(dfile)) == NULL)
- return;
- strcpy(host, &cmdfile[2]);
- host[ strlen(cmdfile)-7 ] = '\0';
-
- sendfailure(from, to, host, hours, dfile);
-
- unlink(subfile(cmdfile));
- unlink(subfile(dfile));
- unlink(subfile(xfile));
-
- return;
- }
-
-
- /*
- * warn - send a warning message to the sender and add the control file
- * to the list of files for which warnings have been sent.
- */
- warn(cmdfile, hours)
- char *cmdfile;
- int hours;
- {
- char dfile[NAMESIZE], xfile[NAMESIZE];
- char host[NAMESIZE];
- char *from, **to;
- char *sender(), **recipients();
-
- if (in_warnedlist(cmdfile))
- return;
-
- DEBUG(4, "warn called on %s\n", cmdfile);
- getfnames(cmdfile, dfile, xfile);
-
- if ((to = recipients(xfile)) == NULL)
- return;
- if ((from = sender(dfile)) == NULL)
- return;
- strcpy(host, &cmdfile[2]);
- host[ strlen(cmdfile)-7 ] = '\0';
-
- sendwarning(from, to, host, hours, failtime, dfile);
-
- add_warnedlist(cmdfile);
-
- return;
- }
-
- /*
- * getfnames - read the control file to find the data and execute files
- * which contain the message and list of recipients.
- * dfile is set to the datafile, xfile to the execute file.
- */
- getfnames(cmdfile, dfile, xfile)
- char *cmdfile;
- char *dfile;
- char *xfile;
- {
- FILE *fp;
- char dline[100], xline[100];
- char *wrkvec[10];
-
- if ((fp = fopen(subfile(cmdfile), "r")) == NULL)
- return;
-
- if (fgets(dline, 100, fp) == NULL || fgets(xline, 100, fp) == NULL)
- { fclose(fp);
- return;
- }
-
- if (getargs(dline, wrkvec) <= QF_INDEX)
- { fclose(fp);
- return;
- }
- strcpy(dfile, wrkvec[ QF_INDEX ]);
-
- if (getargs(xline, wrkvec) < QF_INDEX)
- { fclose(fp);
- return;
- }
- strcpy(xfile, wrkvec[ QF_INDEX ]);
-
- fclose(fp);
- }
-
- /*
- * recipients - returns a list of recipients that the mail was intended
- * for, or NULL if the execute file is not a mail file.
- */
- char **
- recipients(xfile)
- char *xfile;
- {
- static char rbuf[BUFSIZ];
- static char *tobuf[1000]; /* see uuxqt */
- FILE *fp;
- char *p, **t;
-
- if ((fp = fopen(subfile(xfile), "r")) == NULL)
- return(NULL);
-
- while (fgets(rbuf, BUFSIZ, fp) != NULL)
- { if (rbuf[0] == X_CMD)
- { if (strncmp(rbuf, "C rmail ", 8) == SAME)
- { fclose(fp);
-
- /* turn into an array of addresses */
- for (p = &rbuf[8], t=tobuf; *p;)
- { while (*p == ' ' || *p == '\n')
- *p++ = '\0';
- *t = p;
- while (*p && *p != ' ' && *p != '\n')
- p++;
- if (*t != p)
- t++;
- }
- *t = NULL;
- return(tobuf);
- }
- }
- }
-
- fclose(fp);
- return(NULL);
- }
-
- /*
- * sender - returns the sender address from the uucp from line,
- * or NULL if not found.
- */
- char *
- sender(dfile)
- char *dfile;
- {
- static char sender[BUFSIZ];
- char buf[BUFSIZ];
- FILE *fp;
-
- if ((fp = fopen(subfile(dfile), "r")) == NULL)
- return(NULL);
-
- if (fgets(buf, BUFSIZ, fp) == NULL)
- return(NULL);
-
- if (sscanf(buf, "From %s", sender) == 1)
- { fclose(fp);
- return(sender);
- }
-
- fclose(fp);
- return(NULL);
- }
-
-
- /*
- * exists - returns 1 if "file" exists, else 0.
- */
- exists(file)
- char *file;
- {
- return( access(subfile(file),0) == 0 );
- }
-
-
- /*
- * print_message - print the message in "dfile" on the stream "outp".
- * If the edited flag is set, then only print some
- * interesting headers and the first few lines of the body.
- */
- print_message(dfile, outp, edited)
- char *dfile;
- FILE *outp;
- int edited;
- {
- FILE *dfp;
- char buf[BUFSIZ];
- int iflg, linecount;
-
- if ((dfp = fopen(subfile(dfile), "r")) == NULL)
- return;
-
- /* skip unix from line */
- fgets(buf, BUFSIZ, dfp);
-
- /* print header */
- iflg = 0;
- while (fgets(buf, BUFSIZ, dfp) != NULL && buf[0] != '\n')
- { if (edited)
- { if (buf[0] == '\t' || buf[0] == ' ')
- { if (iflg)
- fputs(buf, outp);
- continue;
- }
-
- if (!interested(buf))
- { iflg = 0;
- continue;
- }
- iflg = 1;
- }
- fputs(buf, outp);
- }
- putc('\n', outp);
-
- /* print body */
- linecount = 0;
- while (fgets(buf, BUFSIZ, dfp) != NULL)
- { if (edited && ++linecount > 5)
- { fprintf(outp, ".....\n");
- break;
- }
- fputs(buf, outp);
- }
- fclose(dfp);
- }
-
- static char *headers[] = { "From:", "Date:", "To:", "Cc:", "Subject:", 0 };
-
- /*
- * interested - determine whether "hdr" is considered interesting
- * and thus should be printed in edited mode.
- */
- interested(hdr)
- char *hdr;
- {
- char **hp = headers;
-
- while (*hp)
- { if (strncmp(hdr, *hp, strlen(*hp)) == SAME)
- return(1);
- hp++;
- }
- return(0);
- }
-
-
- cleanup(code)
- int code;
- {
- exit(code);
- }
-